import { message } from "antd";
export const convertSize = (limit: any) => {
  let size: any = 0;
  if (limit < 0.1 * 1024) {
    if (limit < 0.1 * 100) {
      size = 0;
    } else {
      size = limit.toFixed(2) + "B"; //小于0.1KB，则转化成B
    }
  } else if (limit < 0.1 * 1024 * 1024) {
    //小于0.1MB，则转化成KB
    size = (limit / 1024).toFixed(2) + "KB";
  } else if (limit < 0.1 * 1024 * 1024 * 1024) {
    //小于0.1GB，则转化成MB
    size = (limit / (1024 * 1024)).toFixed(2) + "MB";
  } else {
    //其他转化成GB
    size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
  }
  const sizeStr = size + ""; //转成字符串
  const index = sizeStr.indexOf("."); //获取小数点处的索引
  const dou = sizeStr.substr(index + 1, 2); //获取小数点后两位的值
  if (dou == "00") {
    //判断后两位是否为00，如果是则删除00
    return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2);
  }
  return size;
};

// js执行

export function exeScript(model: any) {
  // product header nav bar tab switch
  // 第一步：匹配加载的页面中是否含有js
  const regDetectJs = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/gi;
  const jsContained = model.match(regDetectJs); //ajaxLoadedData为ajax获取到的数据
  // 第二步：如果包含js，则一段一段的取出js再加载执行
  if (jsContained) {
    // 分段取出js正则
    const regGetJS = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im;
    // 按顺序分段执行js
    const jsNums = jsContained.length;
    for (let i = 0; i < jsNums; i++) {
      const jsSection = jsContained[i].match(regGetJS);
      if (jsSection[2]) {
        if (window.execScript) {
          // 给IE的特殊待遇
          window.execScript(jsSection[2]);
        } else {
          // 给其他大部分浏览器用的
          window.eval(jsSection[2]);
        }
      }
    }
  }
}

// 秒转十分秒
export function formatSecToHms(time: any) {
  const h = parseInt((time / 3600).toString());
  const minute = parseInt(((time / 60) % 60).toString());
  const second = Math.ceil(time % 60);

  const hours = h < 10 ? "0" + h : h;
  const formatSecond = second > 59 ? 59 : second;
  return `${hours > 0 ? `${hours}h` : ""}${
    minute < 10 ? (minute == 0 ? "" : "0" + minute + "m") : minute + "m"
  }${
    formatSecond < 10
      ? formatSecond == 0
        ? ""
        : "0" + formatSecond + "s"
      : formatSecond + "s"
  }`;
}

export function IsPhone() {
  //获取浏览器navigator对象的userAgent属性（浏览器用于HTTP请求的用户代理头的值）
  const info = navigator.userAgent;
  //通过正则表达式的test方法判断是否包含“Mobile”字符串
  const isPhone = /mobile/i.test(info);
  //如果包含“Mobile”（是手机设备）则返回true
  return isPhone;
}

// 全屏代码
export function fullScreen() {
  //var isfull = false;
  //if (this.checkFull())
  //    isfull= true;
  const docElm = document.documentElement;

  //FireFox
  // @ts-ignore
  if (docElm.mozRequestFullScreen) {
    // @ts-ignore
    docElm.mozRequestFullScreen();
  }
  //Chrome等
  // @ts-ignore
  else if (docElm.webkitRequestFullScreen) {
    // @ts-ignore
    docElm.webkitRequestFullScreen();
  }
  //IE11
  // @ts-ignore
  else if (docElm.msRequestFullscreen) {
    // @ts-ignore
    docElm.msRequestFullscreen();
  }
  //W3C
  else if (docElm.requestFullscreen) {
    docElm.requestFullscreen();
  } else {
    message.info("浏览器不支持全屏API或已被禁用");
    return false;
  }

  return true;
}

//退出全屏
export function exitFullScreen() {
  // @ts-ignore
  if (document.mozCancelFullScreen) {
    // @ts-ignore
    document.mozCancelFullScreen();
  }
  // @ts-ignore
  else if (document.webkitCancelFullScreen) {
    // @ts-ignore
    document.webkitCancelFullScreen();
  }
  // @ts-ignore
  else if (document.msExitFullscreen) {
    // @ts-ignore
    document.msExitFullscreen();
  } else if (document.exitFullscreen) {
    document.exitFullscreen();
  } else {
    message.info("浏览器不支持全屏API或已被禁用");
    return false;
  }
  return true;
}

// cookie相关设置
export function getCookie(name: any) {
  const nameEQ = name + "=";
  const ca = document.cookie.split(";"); // 把cookie分割成组
  for (let i = 0; i < ca.length; i++) {
    const c = ca[i]; // 取得字符串
    while (c.charAt(0) == " ") {
      // 判断一下字符串有没有前导空格
      c = c.substring(1, c.length); // 有的话，从第二位开始取
    }
    if (c.indexOf(nameEQ) == 0) {
      // 如果含有我们要的name
      return unescape(c.substring(nameEQ.length, c.length)); // 解码并截取我们要值
    }
  }
  return false;
}
// 清除cookie
export function clearCookie(name: any) {
  setCookie(name, "", -1);
}
// 设置cookie
export function setCookie(name: any, value: any, seconds2: any) {
  const seconds = seconds2 || 0; //seconds有值就直接赋值，没有为0，这个根php不一样。
  let expires = "";
  if (seconds != 0) {
    //设置cookie生存时间
    const date: any = new Date();
    date.setTime(date.getTime() + seconds * 1000);
    expires = "; expires=" + date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expires + "; path=/"; //转码并赋值
}

// 邮箱正则
export const emailRe =
  /^(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
